package com.lei.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lei.ssm.Util.SNSResultUtil;
import com.lei.ssm.entity.UserAuthorityEntity;
import com.lei.ssm.service.UserService;
import com.lei.ssm.vo.SNSResult;

@RestController
@RequestMapping("User")
public class UserController {
	@Autowired
	private UserService userService;
	@PutMapping
	public SNSResult<?> update(@RequestBody UserAuthorityEntity user) {
		try {
			userService.update(user);
			return SNSResultUtil.getSuccessResult();
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
	@DeleteMapping
	public SNSResult<?> delete(@RequestParam("id") Long id) {
		if (userService.delete(id)) {
			return SNSResultUtil.getSuccessResult();
		}
		return SNSResultUtil.getErrorResult("删除失败");
	}
	@PostMapping
	public SNSResult<?> add(@RequestBody UserAuthorityEntity user) {
		try {
			userService.add(user);
			return SNSResultUtil.getSuccessResult();
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
	@GetMapping
	public SNSResult<?> getByName(@RequestParam("codeName") String codeName) {
		try {
			return SNSResultUtil.getSuccessResult(userService.getByName(codeName));
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
	@GetMapping("listAll")
	public SNSResult<?> listAll() {
		try {
			return SNSResultUtil.getSuccessResult(userService.listAll());
		} catch (Exception e) {
			e.printStackTrace();
			return SNSResultUtil.getErrorResult();
		}
	}
}
